<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class generate_html extends API_Controller {

    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $site_id = $this->input->get('site_id');
        $t_id = $this->input->get('t_id');
        $color = $this->input->get('color');
        $this->generate_home_html($site_id, $t_id, $color);
        $this->generate_item1_html($site_id, $t_id, $color);
        $this->generate_item2_html($site_id, $t_id, $color);
        $this->generate_item3_html($site_id);
        $this->generate_item4_html($site_id);

        $url = base_url()."$site_id";

        $qrcode = $this->qrcode($url, $site_id); //二维码
        $data = array(
            'url' => $url,
            'qrcode' => $qrcode,

        );
        $this->load->view("templates/sitesuccess.php", $data);
    }
    /**
     * 生成首页
     */
    public function generate_home_html($site_id, $t_id, $color)
    {
        $base_url = base_url().$site_id."/item";
 		$base_url = urlencode($base_url);
        $url = base_url()."index.php/templates/set_home_template?site_id=".$site_id."&t_id=".$t_id."&color=".$color."&baseurl=".$base_url;
        $html = file_get_contents($url);
        $this->save($site_id, 'index', $html);
    }

    /**
     * 生成栏目一
     */
    public function generate_item1_html($site_id, $t_id, $color)
    {
        $url = base_url()."index.php/templates/set_item1?site_id=".$site_id."&t_id=".$t_id."&color=".$color."&static=1";
        $html = file_get_contents($url);
        $this->save($site_id, 'item1', $html);
    }

    /**
     * 生成栏目二
     */
    public function generate_item2_html($site_id, $t_id, $color)
    {
        $url = base_url()."index.php/templates/set_item2?site_id=".$site_id."&t_id=".$t_id."&color=".$color."&static=1";
        $html = file_get_contents($url);
        $this->save($site_id, 'item2', $html);
    }

    /**
     * 生成栏目三
     */
    public function generate_item3_html($site_id)
    {
        $url = base_url()."index.php/templates/set_item3?site_id=".$site_id."&t_id=".$t_id."&color=".$color."&static=1";
        $html = file_get_contents($url);
        $this->save($site_id, 'item3', $html);
    }

    /**
     * 生成栏目四
     */
    public function generate_item4_html($site_id)
    {
        $url = base_url()."index.php/templates/set_item4?site_id=".$site_id."&t_id=".$t_id."&color=".$color."&static=1";
        $html = file_get_contents($url);
        $this->save($site_id, 'item4', $html);
    }

    /**
     * 生成二维码
     */
    public function qrcode($data, $site_id)
    {
        //引入phpqrcode库文件
        include('phpqrcode.php');
        // 二维码数据
        //$data = 'http://www.baidu.com';
        // 生成的文件名
        $filename = $site_id."/qrcode.png";
        //$filename = "baidu.png";
        // 纠错级别：L、M、Q、H
        $errorCorrectionLevel = 'L';
        // 点的大小：1到10
        $matrixPointSize = 4;
        //创建一个二维码文件
        QRcode::png($data, $filename, $errorCorrectionLevel, $matrixPointSize, 2);

        return $filename;
        //输入二维码到浏览器
        //QRcode::png($data);
    }

    /**
     * html存放路径
     */
    public function save($site_id, $file, $data)
    {
        $file_path = BASE_PATH.'/webroot/'.$site_id;
        if (!file_exists($file_path)) {
                mkdir($file_path, 0777, TRUE);
        }
        file_put_contents($file_path.'/'.$file.".html", $data);
    }
}
